# 简化版测试脚本
# 网络安全漏洞知识图谱 - 无Docker测试

Write-Host "=== 网络安全漏洞知识图谱测试环境 ===" -ForegroundColor Blue

# 1. 创建目录结构
Write-Host "1. 创建目录结构..." -ForegroundColor Yellow

$testDirs = @(
    "tests\data\raw",
    "tests\data\processed", 
    "tests\data\expected",
    "tests\data\mock_apis",
    "tests\logs",
    "tests\reports",
    "tests\unit",
    "tests\integration",
    "tests\config"
)

foreach ($dir in $testDirs) {
    if (!(Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
        Write-Host "   ✅ 创建: $dir" -ForegroundColor Green
    } else {
        Write-Host "   ℹ️ 已存在: $dir" -ForegroundColor Cyan
    }
}

# 2. 生成测试数据
Write-Host "2. 生成测试数据..." -ForegroundColor Yellow

# JSON测试数据
$jsonData = @{
    cve_data = @(
        @{
            id = "CVE-2023-TEST-001"
            title = "Test Buffer Overflow"
            severity = "HIGH"
            cvss_score = 8.5
        },
        @{
            id = "CVE-2023-TEST-002"
            title = "Test SQL Injection" 
            severity = "MEDIUM"
            cvss_score = 6.8
        }
    )
} | ConvertTo-Json -Depth 3

$jsonData | Out-File -FilePath "tests\data\mock_apis\test_data.json" -Encoding UTF8
Write-Host "   ✅ JSON数据: tests\data\mock_apis\test_data.json" -ForegroundColor Green

# CSV测试数据
$csvContent = @"
cve_id,title,severity,cvss_score
CVE-2023-TEST-001,Test Vulnerability 1,HIGH,8.5
CVE-2023-TEST-002,Test Vulnerability 2,MEDIUM,6.8
CVE-2023-TEST-003,Test Vulnerability 3,CRITICAL,9.2
"@

$csvContent | Out-File -FilePath "tests\data\raw\test_vulnerabilities.csv" -Encoding UTF8
Write-Host "   ✅ CSV数据: tests\data\raw\test_vulnerabilities.csv" -ForegroundColor Green

# 3. 运行基础测试
Write-Host "3. 运行基础测试..." -ForegroundColor Yellow

$testResults = @{}

# 测试1: 文件读写
try {
    $testFile = "tests\data\temp_test.txt"
    "测试内容" | Out-File -FilePath $testFile -Encoding UTF8
    $content = Get-Content $testFile -Encoding UTF8
    Remove-Item $testFile -Force
    
    if ($content -eq "测试内容") {
        Write-Host "   ✅ 文件读写测试: 通过" -ForegroundColor Green
        $testResults["file_test"] = "PASS"
    } else {
        Write-Host "   ❌ 文件读写测试: 失败" -ForegroundColor Red
        $testResults["file_test"] = "FAIL"
    }
} catch {
    Write-Host "   ❌ 文件读写测试: 异常 - $($_.Exception.Message)" -ForegroundColor Red
    $testResults["file_test"] = "ERROR"
}

# 测试2: JSON处理
try {
    $testJson = @{ "test" = "data" } | ConvertTo-Json
    $parsed = $testJson | ConvertFrom-Json
    
    if ($parsed.test -eq "data") {
        Write-Host "   ✅ JSON处理测试: 通过" -ForegroundColor Green
        $testResults["json_test"] = "PASS"
    } else {
        Write-Host "   ❌ JSON处理测试: 失败" -ForegroundColor Red
        $testResults["json_test"] = "FAIL"
    }
} catch {
    Write-Host "   ❌ JSON处理测试: 异常 - $($_.Exception.Message)" -ForegroundColor Red
    $testResults["json_test"] = "ERROR"
}

# 测试3: 目录检查
$requiredDirs = @("tests\data", "tests\logs", "tests\reports")
$allDirsExist = $true
foreach ($dir in $requiredDirs) {
    if (!(Test-Path $dir)) {
        $allDirsExist = $false
        break
    }
}

if ($allDirsExist) {
    Write-Host "   ✅ 目录结构测试: 通过" -ForegroundColor Green
    $testResults["dir_test"] = "PASS"
} else {
    Write-Host "   ❌ 目录结构测试: 失败" -ForegroundColor Red
    $testResults["dir_test"] = "FAIL"
}

# 4. 检查Python环境
Write-Host "4. 检查Python环境..." -ForegroundColor Yellow

$pythonAvailable = $false
try {
    $pythonVersion = python --version 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✅ Python可用: $pythonVersion" -ForegroundColor Green
        $pythonAvailable = $true
    }
} catch {
    try {
        $pythonVersion = py --version 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Host "   ✅ Python可用 (py命令): $pythonVersion" -ForegroundColor Green
            $pythonAvailable = $true
        }
    } catch {
        Write-Host "   ⚠️ Python未安装或不在PATH中" -ForegroundColor Yellow
    }
}

if (!$pythonAvailable) {
    Write-Host "   ℹ️ 跳过Python相关测试" -ForegroundColor Cyan
}

# 5. 生成测试报告
Write-Host "5. 生成测试报告..." -ForegroundColor Yellow

$reportTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$reportData = @{
    timestamp = $reportTime
    environment = @{
        os = $env:OS
        powershell_version = $PSVersionTable.PSVersion.ToString()
        python_available = $pythonAvailable
    }
    test_results = $testResults
    files_created = @(
        "tests\data\mock_apis\test_data.json",
        "tests\data\raw\test_vulnerabilities.csv"
    )
}

$reportJson = $reportData | ConvertTo-Json -Depth 3
$reportPath = "tests\reports\test_report_$(Get-Date -Format 'yyyyMMdd_HHmmss').json"
$reportJson | Out-File -FilePath $reportPath -Encoding UTF8

Write-Host "   ✅ JSON报告: $reportPath" -ForegroundColor Green

# 生成HTML报告
$htmlContent = @"
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>测试报告 - $reportTime</title>
    <style>
        body { font-family: 'Microsoft YaHei', Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .header { background: #3498db; color: white; padding: 20px; border-radius: 5px; text-align: center; margin-bottom: 20px; }
        .section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .success { background: #d4edda; border-color: #c3e6cb; }
        .warning { background: #fff3cd; border-color: #ffeaa7; }
        .info { background: #d1ecf1; border-color: #bee5eb; }
        .pass { color: #28a745; font-weight: bold; }
        .fail { color: #dc3545; font-weight: bold; }
        .error { color: #fd7e14; font-weight: bold; }
        ul { margin: 10px 0; padding-left: 20px; }
        li { margin: 5px 0; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🛡️ 网络安全漏洞知识图谱测试报告</h1>
            <p>生成时间: $reportTime</p>
        </div>
        
        <div class="section success">
            <h2>✅ 测试环境设置完成</h2>
            <p>本地测试环境已成功设置，无需Docker依赖！</p>
        </div>
        
        <div class="section info">
            <h2>📊 测试结果</h2>
            <ul>
                <li>文件读写测试: <span class="$($testResults['file_test'].ToLower())">$($testResults['file_test'])</span></li>
                <li>JSON处理测试: <span class="$($testResults['json_test'].ToLower())">$($testResults['json_test'])</span></li>
                <li>目录结构测试: <span class="$($testResults['dir_test'].ToLower())">$($testResults['dir_test'])</span></li>
                <li>Python环境: $(if ($pythonAvailable) { "可用" } else { "未安装" })</li>
            </ul>
        </div>
        
        <div class="section">
            <h2>📁 生成的文件</h2>
            <ul>
                <li>JSON测试数据: tests\data\mock_apis\test_data.json</li>
                <li>CSV测试数据: tests\data\raw\test_vulnerabilities.csv</li>
                <li>测试报告: $reportPath</li>
            </ul>
        </div>
        
        <div class="section warning">
            <h2>🔧 下一步操作</h2>
            <ol>
                <li>查看生成的测试数据文件</li>
                <li>配置实际的业务逻辑代码</li>
                <li>如需Python测试，请安装Python环境</li>
                <li>集成真实的漏洞数据源</li>
            </ol>
        </div>
    </div>
</body>
</html>
"@

$htmlPath = "tests\reports\test_report.html"
$htmlContent | Out-File -FilePath $htmlPath -Encoding UTF8
Write-Host "   ✅ HTML报告: $htmlPath" -ForegroundColor Green

# 6. 总结和建议
Write-Host ""
Write-Host "🎉 测试完成！" -ForegroundColor Green
Write-Host ""
Write-Host "📊 查看结果:" -ForegroundColor Cyan
Write-Host "   - 在浏览器中打开: tests\reports\test_report.html" -ForegroundColor White
Write-Host "   - 交互式指南: tests\quick_test_guide.html" -ForegroundColor White
Write-Host ""
Write-Host "🔧 其他操作:" -ForegroundColor Cyan
Write-Host "   - 重新运行: .\tests\simple_test.ps1" -ForegroundColor White
Write-Host "   - 查看数据: notepad tests\data\mock_apis\test_data.json" -ForegroundColor White
Write-Host ""

# 统计测试结果
$passCount = ($testResults.Values | Where-Object { $_ -eq "PASS" }).Count
$totalCount = $testResults.Count

if ($passCount -eq $totalCount) {
    Write-Host "✅ 所有测试通过！($passCount/$totalCount)" -ForegroundColor Green
} else {
    Write-Host "⚠️ 部分测试通过 ($passCount/$totalCount)" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "测试环境已准备就绪，可以开始开发和测试网络安全漏洞知识图谱项目！" -ForegroundColor Blue 